package com.thtf.openguess.service.impl;

import com.google.common.base.Stopwatch;
import com.thtf.openguess.aop.annotation.Time;
import com.thtf.openguess.openguess.C2016000000B204121b;
import com.thtf.openguess.openguess.C2016000000B204122b;
import com.thtf.openguess.openguess.C2016000000B204122b;
import com.thtf.openguess.mapper.C2016000000B204122bMapper;
import com.thtf.openguess.service.AsyncService;
import com.thtf.openguess.service.C2016000000B204122bService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.thtf.openguess.util.Util;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import uk.co.jemos.podam.api.PodamFactory;

import javax.annotation.Resource;
import java.util.*;
import java.util.concurrent.CountDownLatch;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author auth
 * @since 2022-05-27
 */
@Service
public class C2016000000B204122bServiceImpl extends ServiceImpl<C2016000000B204122bMapper, C2016000000B204122b> implements C2016000000B204122bService {

    @Resource
    private AsyncService asyncService;
    @Override
    @Time
    public String insertLonely(Integer num) {
        for (int i=0 ; i<num ; i ++){
            C2016000000B204122b c2016000000B204122b = Util.forEachSetValue(new C2016000000B204122b());
            this.save(c2016000000B204122b);
        }
        return null;
    }

    @Override
    @Time
    public String insertBatch(Integer num) {
        List<C2016000000B204122b> list = new ArrayList<>();
        for (int i=0 ; i<num ; i ++){
            C2016000000B204122b c2016000000B204122b = Util.forEachSetValue(new C2016000000B204122b());
            list.add(c2016000000B204122b);
        }
        this.saveBatch(list);
        return null;
    }

    @Override
    @Time
    public String insertThread(Integer sqlNum, Integer threadNum) throws InterruptedException {
        CountDownLatch countDownLatch = new CountDownLatch(threadNum);
        Stopwatch started = Stopwatch.createStarted();
        for (int i = 1; i <= threadNum; i++) {
            // 第j次提交步长
            List<C2016000000B204122b> list= new ArrayList<>();
            for (int j = 1; j <= sqlNum; j++) {
                C2016000000B204122b c2016000000B204122b = Util.forEachSetValue(new C2016000000B204122b());
                list.add(c2016000000B204122b);
            }
            // 添加执行SQL
            asyncService.dealAdd(list, countDownLatch, sqlNum,this);

        }
        countDownLatch.await();
        return null;
    }

    @Override
    @Time
    public String insertZzjgdm(Integer sqlNum,String zzjgdm) {
        List<C2016000000B204122b> list = new ArrayList<>();
        for (int i=0 ; i<sqlNum ; i ++){
            C2016000000B204122b c2016000000B204122b = Util.forEachSetValue(new C2016000000B204122b());
            c2016000000B204122b.setZzjgdm(zzjgdm);
            list.add(c2016000000B204122b);
        }
        this.saveBatch(list);
        return null;
    }

    @Override
    @Time
    public String updateSingle(String zzjgdm, Integer type) {
        Collection<C2016000000B204122b> c2016000000B204122bs = this.selectList(zzjgdm);
        c2016000000B204122bs.forEach(c2016000000B204122b -> {
            C2016000000B204122b c2016000000B204122bA=new C2016000000B204122b();
            c2016000000B204122bA.setBgq(c2016000000B204122b.getBgq());
            switch (type){
                case 1: this.addLess(c2016000000B204122bA); break;
                case 2: this.addMore(c2016000000B204122bA); break;
                case 3: this.addAll(c2016000000B204122bA); break;
            }
            this.updateById(c2016000000B204122bA);
        });

        return null;
    }

    @Override
    @Time
    public String updatebatch(String zzjgdm, Integer type) {
        Collection<C2016000000B204122b> c2016000000B204122bs = this.selectList(zzjgdm);
        List<C2016000000B204122b> list = new ArrayList<>();
        c2016000000B204122bs.forEach(c2016000000B204122b -> {
            C2016000000B204122b c2016000000B204122bA=new C2016000000B204122b();
            c2016000000B204122bA.setBgq(c2016000000B204122b.getBgq());
            switch (type){
                case 1: this.addLess(c2016000000B204122bA); break;
                case 2: this.addMore(c2016000000B204122bA); break;
                case 3: this.addAll(c2016000000B204122bA); break;
            }
            list.add(c2016000000B204122bA);
        });
        this.updateBatchById(list);
        return null;
    }

    @Override
    @Time
    public String deleteSingle(String zzjgdm) {
        Collection<C2016000000B204122b> c2016000000B204122bs = this.selectList(zzjgdm);
        c2016000000B204122bs.forEach(c2016000000B204122b -> {
            this.removeById(c2016000000B204122b.getBgq());
        });
        return null;
    }

    @Override
    @Time
    public String deleteBatch(String zzjgdm) {
        Collection<C2016000000B204122b> c2016000000B204122bs = this.selectList(zzjgdm);
        List<String> list=new ArrayList<>();
        c2016000000B204122bs.forEach(c2016000000B204122b -> {
            list.add(c2016000000B204122b.getBgq());
        });
        this.removeByIds(list);
        return null;
    }

    private void addLess(C2016000000B204122b c2016000000B204122bA) {
        c2016000000B204122bA.setC_1(Util.ramdom());
        c2016000000B204122bA.setC_2(Util.ramdom());
    }

    private void addMore(C2016000000B204122b c2016000000B204122bA) {
        this.addLess(c2016000000B204122bA);
        c2016000000B204122bA.setC_3(Util.ramdom());
        c2016000000B204122bA.setC_4(Util.ramdom());
        c2016000000B204122bA.setTjjg(Util.ramdom().toString());

    }

    private void addAll(C2016000000B204122b c2016000000B204122bA) {
        C2016000000B204122b c2016000000B204122b = Util.forEachSetValue(new C2016000000B204122b());
        c2016000000B204122b.setBgq(c2016000000B204122bA.getBgq());
        c2016000000B204122b.setZzjgdm(c2016000000B204122bA.getZzjgdm());
        c2016000000B204122b.setDcdxxtm(c2016000000B204122bA.getDcdxxtm());
        BeanUtils.copyProperties(c2016000000B204122b,c2016000000B204122bA);
    }

    private Collection<C2016000000B204122b> selectList(String zzjgdm){
        Map<String ,Object> map =new HashMap<>();
        map.put("zzjgdm",zzjgdm);
        return this.listByMap(map);
    }


}
